summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GpuDriverHelper.kt
blob: bf26ab51e8ab34e50b99c5620cac049eef3f1b7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.utils

import android.content.Context
import android.net.Uri
import org.yuzu.yuzu_emu.NativeLibrary
import org.yuzu.yuzu_emu.utils.FileUtil.copyUriToInternalStorage
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.util.zip.ZipInputStream

object GpuDriverHelper {
    private const val META_JSON_FILENAME = "meta.json"
    private const val DRIVER_INTERNAL_FILENAME = "gpu_driver.zip"
    private var fileRedirectionPath: String? = null
    private var driverInstallationPath: String? = null
    private var hookLibPath: String? = null

    @Throws(IOException::class)
    private fun unzip(zipFilePath: String, destDir: String) {
        val dir = File(destDir)

        // Create output directory if it doesn't exist
        if (!dir.exists()) dir.mkdirs()

        // Unpack the files.
        val zis = ZipInputStream(FileInputStream(zipFilePath))
        val buffer = ByteArray(1024)
        var ze = zis.nextEntry
        while (ze != null) {
            val fileName = ze.name
            val newFile = File(destDir + fileName)
            newFile.parentFile!!.mkdirs()
            val fos = FileOutputStream(newFile)
            var len: Int
            while (zis.read(buffer).also { len = it } > 0) {
                fos.write(buffer, 0, len)
            }
            fos.close()
            zis.closeEntry()
            ze = zis.nextEntry
        }
        zis.closeEntry()
    }

    @JvmStatic
    fun initializeDriverParameters(context: Context) {
        try {
            // Initialize the file redirection directory.
            fileRedirectionPath =
                context.getExternalFilesDir(null)!!.canonicalPath + "/gpu/vk_file_redirect/"

            // Initialize the driver installation directory.
            driverInstallationPath = context.filesDir.canonicalPath + "/gpu_driver/"
        } catch (e: IOException) {
            throw RuntimeException(e)
        }

        // Initialize directories.
        initializeDirectories()

        // Initialize hook libraries directory.
        hookLibPath = context.applicationInfo.nativeLibraryDir + "/"

        // Initialize GPU driver.
        NativeLibrary.InitializeGpuDriver(
            hookLibPath,
            driverInstallationPath,
            customDriverLibraryName,
            fileRedirectionPath
        )
    }

    fun installDefaultDriver(context: Context) {
        // Removing the installed driver will result in the backend using the default system driver.
        val driverInstallationDir = File(driverInstallationPath!!)
        deleteRecursive(driverInstallationDir)
        initializeDriverParameters(context)
    }

    fun installCustomDriver(context: Context, driverPathUri: Uri?) {
        // Revert to system default in the event the specified driver is bad.
        installDefaultDriver(context)

        // Ensure we have directories.
        initializeDirectories()

        // Copy the zip file URI into our private storage.
        copyUriToInternalStorage(
            context,
            driverPathUri,
            driverInstallationPath!!,
            DRIVER_INTERNAL_FILENAME
        )

        // Unzip the driver.
        try {
            unzip(driverInstallationPath + DRIVER_INTERNAL_FILENAME, driverInstallationPath!!)
        } catch (e: IOException) {
            throw RuntimeException(e)
        }

        // Initialize the driver parameters.
        initializeDriverParameters(context)
    }

    // Parse the custom driver metadata to retrieve the name.
    val customDriverName: String?
        get() {
            // Parse the custom driver metadata to retrieve the name.
            val metadata = GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME)
            return metadata.name
        }

    // Parse the custom driver metadata to retrieve the library name.
    private val customDriverLibraryName: String?
        get() {
            // Parse the custom driver metadata to retrieve the library name.
            val metadata = GpuDriverMetadata(driverInstallationPath + META_JSON_FILENAME)
            return metadata.libraryName
        }

    private fun initializeDirectories() {
        // Ensure the file redirection directory exists.
        val fileRedirectionDir = File(fileRedirectionPath!!)
        if (!fileRedirectionDir.exists()) {
            fileRedirectionDir.mkdirs()
        }
        // Ensure the driver installation directory exists.
        val driverInstallationDir = File(driverInstallationPath!!)
        if (!driverInstallationDir.exists()) {
            driverInstallationDir.mkdirs()
        }
    }

    private fun deleteRecursive(fileOrDirectory: File) {
        if (fileOrDirectory.isDirectory) {
            for (child in fileOrDirectory.listFiles()!!) {
                deleteRecursive(child)
            }
        }
        fileOrDirectory.delete()
    }
}